package id.net.iconpln.bb.client.view;


import id.net.iconpln.bb.client.service.MasterRPCService;
import id.net.iconpln.bb.client.service.MasterRPCServiceAsync;
import id.net.iconpln.bb.client.service.TransportRPCService;
import id.net.iconpln.bb.client.service.TransportRPCServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbTransportPengiriman extends LayoutContainer {
	private final MasterRPCServiceAsync serviceMaster = GWT.create(MasterRPCService.class);
	private final TransportRPCServiceAsync service = GWT.create(TransportRPCService.class);
	private Grid gridPengajuan, gridShipment, gridLoading;
	private Grid gridPembangkit;
	private ListStore storePembangkit, storePengajuan, storeShipment, storeLoading;
	
	  @Override  
	  protected void onRender(Element parent, int index) {  
	    super.onRender(parent, index);  
	    setLayout(new FitLayout());
	    
	    ContentPanel cp = new ContentPanel();  	   
	    cp.setHeaderVisible(false);
	    cp.setFrame(true);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL)); 
	    
	    createGridPembangkit();
	    gridPembangkit.getSelectionModel().addListener(Events.SelectionChange,  
    		new Listener<SelectionChangedEvent>() {  
    		public void handleEvent(SelectionChangedEvent be) {  			  
    			getListPengajuan("30", gridPembangkit.getSelectionModel().getSelectedItem().get("id_pltu").toString(), "ENTRY");
    			getListPengajuan("30", gridPembangkit.getSelectionModel().getSelectedItem().get("id_pltu").toString(), "SHIPMENT");
    			getListPengajuan("30", gridPembangkit.getSelectionModel().getSelectedItem().get("id_pltu").toString(), "LOADING");
    		}  
        });
	    
	    ToolBar tbPembangkit = new ToolBar();
	    tbPembangkit.add(new LabelToolItem("<b>List Pembangkit</b>"));
	    tbPembangkit.add(new SeparatorToolItem());
	    
	    ContentPanel pg2 = new ContentPanel();
	    pg2.setHeaderVisible(false);
	    pg2.setLayout(new RowLayout(Orientation.HORIZONTAL)); 
	    pg2.add(gridPembangkit, new RowData(1, 1));
	    pg2.setTopComponent(tbPembangkit);
	    cp.add(pg2, new RowData(.4, 1));
	    
	    ContentPanel pg1 = new ContentPanel();
	    pg1.setLayout(new FitLayout()); 
	    pg1.setHeight(400);
	    pg1.setHeaderVisible(false);
	  
	    createGridPengajuan(); 
	    gridPengajuan.setHeight(400);
	    pg1.add(gridPengajuan,new RowData(1, 1));
	    
	    final TabPanel tabs = new TabPanel();  
	    
	    TabItem personal = new TabItem();  
	    personal.setStyleAttribute("padding", "10px");  
	    personal.setText("Data Pengajuan");  
	    personal.setLayout(new FormLayout());  
	    personal.add(gridPengajuan,new RowData(1, 1));
	    
	    TabItem shipment = new TabItem();  
	    shipment.setStyleAttribute("padding", "10px");  
	    shipment.setText("Data Shipment");  
	    shipment.setLayout(new FormLayout());  
	    shipment.add(gridShipment,new RowData(1, 1));
	    
	    TabItem loading = new TabItem();  
	    loading.setStyleAttribute("padding", "10px");  
	    loading.setText("Data Loading");  
	    loading.setLayout(new FormLayout());  
	    loading.add(gridShipment,new RowData(1, 1));
	    
	    tabs.add(personal);
	    tabs.add(shipment);
	    tabs.add(loading);
	    
	    
	    
	    ContentPanel delimeter = new ContentPanel();
	    delimeter.setHeaderVisible(false);
	    delimeter.setWidth(10);
	    cp.add(delimeter, new FitData(5));
	    cp.add(tabs, new RowData(.6, 1));
	    
	    add(cp);  
	  }  
	  
	  private void getListPengajuan(String id_pemasok, String id_pltu, final String status){
		  Map mapParam = new FastMap();
  		  mapParam.put("id_pemasok", id_pemasok);
  		  mapParam.put("id_pltu", id_pltu);
  		  mapParam.put("status", status);
  		  GWT.log(id_pemasok + id_pltu + status , null);
  		  
  		  service.getListPengiriman("PENGAJUAN_BY_PLTU_PEMASOK", mapParam,new AsyncCallback<List<Map>>() {
  			public void onFailure(Throwable caught) {
  				GWT.log(caught.getMessage(), null);
  			}

  			public void onSuccess(List<Map> result) {	
  				GWT.log("result : " + result.size() , null);
  				if (status.equals("ENTRY")){
  					storePengajuan.removeAll();
  	  				storePengajuan.add(ClientModule.setListBaseModelFromListMap(result));
  				}else if (status.equals("SHIPMENT")){
  					storeShipment.removeAll();
  	  				storeShipment.add(ClientModule.setListBaseModelFromListMap(result));
  				}else if (status.equals("LOADING")){
  					storeLoading.removeAll();
  	  				storeLoading.add(ClientModule.setListBaseModelFromListMap(result));
  				}  				
  			}
  		});
	  }
	  
	  private void createGridPembangkit(){
		    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
		    ColumnConfig column = new ColumnConfig();	

		    column = new ColumnConfig("nama_pembangkit", 250);  
		    column.setHeader("Pembangkit");    
		    configs.add(column);  
		    
		    storePembangkit = new ListStore();  
		    storePembangkit.setMonitorChanges(true);  
		    serviceMaster.getMaster("KIT", new FastMap(), new AsyncCallback<List<Map>>() {
				
				@Override
				public void onSuccess(List<Map> result) {
					// TODO Auto-generated method stub
					storePembangkit.removeAll();
					storePembangkit.add(ClientModule.setListBaseModelFromListMap(result));
				}
				
				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					GWT.log(caught.getMessage());
				}
			});
		    
		    ColumnModel cm = new ColumnModel(configs);  
		  
		    gridPembangkit = new Grid(storePembangkit, cm);  
		    gridPembangkit.getView().setEmptyText("Data Empty");  
		    gridPembangkit.setBorders(true);  
	  }   

	  private void createGridPengajuan() {  
		  storePengajuan = new ListStore();
		  storeShipment = new ListStore();
		  storeLoading = new ListStore();
		  List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  

		    ColumnConfig column = new ColumnConfig();	 
		    column = new ColumnConfig("no_jadwal", "No Jadwal", 150);  
		    configs.add(column);  
		    
		    column = new ColumnConfig("no_pengajuan", "No Pengajuan", 150);  
		    configs.add(column); 

		    column = new ColumnConfig("bulan_tahun", "BLTH", 100);  
		    configs.add(column);  	  
		    		  
		    column = new ColumnConfig("tanggal", "Tanggal", 100);  
		    configs.add(column);
		    
		    column = new ColumnConfig("nama_transportir", "Transportir", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("nama_kapal", "Kapal", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("volume", "Vol (Ton)", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("eta", "ETA", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("etd", "ETD", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("status", "Status", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("keterangan", "Keterangan", 150);  
		    configs.add(column);
		    storePengajuan.setMonitorChanges(true);  
		    ColumnModel cm = new ColumnModel(configs);
		    gridPengajuan = new Grid(storePengajuan, cm);  
		    gridPengajuan.getView().setEmptyText("Data Empty");  
		    gridPengajuan.setBorders(true);  	
		    gridPengajuan.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);  
		    
		    gridShipment  = new Grid(storeShipment, cm);  
		    gridShipment.getView().setEmptyText("Data Empty");  
		    gridShipment.setBorders(true);  	
		    gridShipment.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);  
		    gridShipment.setHeight(400);
		    
		    gridLoading  = new Grid(storeLoading, cm);  
		    gridLoading.getView().setEmptyText("Data Empty");  
		    gridLoading.setBorders(true);  	
		    gridLoading.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);  
		    gridLoading.setHeight(400);
	  }  
}
